//
//  LYHomeViewController.m
//  artist
//
//  Created by Jay_Apple on 16/2/29.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYHomeViewController.h"
#import "MyHomeBtn.h"
#import "UIBarButtonItem+InitWithCustomView.h"
#import "LYMineSegement.h"
#import "LYEArtistViewController.h"
#import "LYCityViewController.h"
#import "LYFriendViewController.h"
#import "LYBadgeBtn.h"
#import "MainViewController.h"
#import "LYTabBarController.h"
#import "VVYLocationManager.h"
#import "LYPublicTextViewController.h"
#import "LYCityDataDBTool.h"

@interface LYHomeViewController ()

//badgeView
@property (weak, nonatomic) UIImageView *badgeView;

//E术家VC
@property (weak, nonatomic) LYEArtistViewController *artistVC;

//艺友圈VC
@property (weak, nonatomic) LYFriendViewController *friendVC;

//发布item
@property (strong, nonatomic) UIBarButtonItem *publishItem;

//消息item
@property (strong, nonatomic) UIBarButtonItem *messageItem;

//定位
@property (nonatomic, strong) UIBarButtonItem *location;

//定位城市
@property (nonatomic, copy) NSString *city;

//当前选中控制器
@property (nonatomic, weak) UIViewController *selectedVC;


@end

@implementation LYHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化导航栏
    [self setUpNav];
    
    //禁止自动调整scrollView
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //注册主题通知
    [self setUpThemeNotification];
    
    //注册消息通知
    [self setUpMessageNotification];
    
    //定位
    [self locate];
}




- (void)dealloc{
    //释放通知
    
    [self resignThemeNotification];
    [self resignMessageNotification];
    
}
#pragma mark - 重写
/**
 *  主题元素定义
 */
- (void)upDateTheme{
    
    if (self.selectedVC == self.artistVC) {
        return;
    }
    
    if ([LYDataSourceManager sharedManager].type == roleTypeStu) {
        
        self.navigationItem.rightBarButtonItem = self.publishItem;
        
    }else{
        
        self.navigationItem.rightBarButtonItem = self.messageItem;
    }
    
    
    
}

/**
 *  定位城市
 */
- (void)setCity:(NSString *)city{
    
    if (city.length == 0) {
        return;
    }
    
    _city = [city copy];
    //设置对应城市显示
    self.location.title = @"";
    self.location.title = city;
    
    
    //通知
    NSInteger city_id = [LYCityDataDBTool getSuperCityIDWithAreaName:city];

    [[NSNotificationCenter defaultCenter] postNotificationName:LYCityChangeNotification object:nil userInfo:@{@"city_id" : @(city_id)}];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.navigationController.navigationBar layoutIfNeeded];
    }];
}


#pragma mark - 懒加载

- (LYFriendViewController *)friendVC{
    
    if (_friendVC == nil) {
        LYFriendViewController *friendVC = [[LYFriendViewController alloc] init];
        [self addChildViewController:friendVC];
        _friendVC = friendVC;
    }
    
    return _friendVC;
}

- (LYEArtistViewController *)artistVC{
    
    if (_artistVC == nil) {
        LYEArtistViewController *artistVC = [[LYEArtistViewController alloc] init];
        [self addChildViewController:artistVC];
        _artistVC = artistVC;
    }
    return _artistVC;
}

- (UIBarButtonItem *)messageItem{
    
    if (_messageItem == nil) {
        LYBadgeBtn * rightBtn = [[LYBadgeBtn alloc] init];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"信息"] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(rightButtonDidPress) forControlEvents:UIControlEventTouchUpInside];
        [rightBtn sizeToFit];
        
        _messageItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        
        
    }
    
    return _messageItem;
    
}


- (void)rightButtonDidPress{

    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
        
        MainViewController * lsvc = [[MainViewController alloc]init];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        LYTabBarController *tab = (LYTabBarController *)window.rootViewController;
        UINavigationController *nav =  tab.selectedViewController;
        [nav pushViewController:lsvc animated:YES];

    }];
}



- (UIBarButtonItem *)publishItem{

    if (_publishItem == nil) {
    
     UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    //右边item
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"发帖"] forState:UIControlStateNormal];
    [rightBtn sizeToFit];
    
    [rightBtn addTarget:self action:@selector(publish) forControlEvents:UIControlEventTouchUpInside];
    _publishItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

    }
    
    return _publishItem;

}
#pragma mark - 初始化

/**
 *  定位
 */
- (void)locate{

    [[VVYLocationManager sharedManager] getLocationCityInfoSuccess:^(NSString *city, CLLocationCoordinate2D coordinate) {
        
        self.city = city;
        [[LYDataSourceManager sharedManager] setUpLocationInfo];
        
    } failure:^(NSString *error) {
        
        [MBProgressHUD showOnlyText:error];
        
    }];

}


/**
 *  初始化导航栏
 */
- (void)setUpNav{
    
    //左Item
    //    MyHomeBtn *leftBtn = [[MyHomeBtn alloc] init];
    //    [leftBtn setTitle:@"正在定位" forState:UIControlStateNormal];
    //    [leftBtn setTitleColor:[UIColor colorWithHexString:LYHomeNavLeftItemTextColor alpha:1.0] forState:UIControlStateNormal];
    //    leftBtn.titleLabel.font = [UIFont systemFontOfSize:LYHomeNavLeftItemTextFont];
    //    [leftBtn setImage:[UIImage imageNamed:@"区域筛选图标"] forState:UIControlStateNormal];
    //    [leftBtn sizeThatFits:CGSizeMake(50, 50)];
    //    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    //    self.navigationItem.leftBarButtonItem = leftItem;
    UIBarButtonItem *location = [[UIBarButtonItem alloc] initWithTitle:@"正在定位" style:UIBarButtonItemStyleDone target:self action:@selector(homeBarLocationBtnOnclick)];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:LYHomeNavLeftItemTextFont];
    dict[NSForegroundColorAttributeName] = [UIColor colorWithHexString:LYHomeNavLeftItemTextColor alpha:1];
    [location setTitleTextAttributes:dict forState:UIControlStateNormal];
    [location setTitleTextAttributes:dict forState:UIControlStateHighlighted];
    self.location = location;
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"地区选中"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"地区选中"] forState:UIControlStateHighlighted];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
    [leftBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [leftBtn sizeToFit];
    [leftBtn addTarget:self action:@selector(homeBarLocationBtnOnclick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *image = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItems = @[location,image];
    
    //segment
    LYMineSegement *segement = [LYMineSegement viewForNib];
    segement.block = ^(SegementType type){
        
        [self changeControllerWithType:type];
        
    };
    segement.My_Size = CGSizeMake(LYHomeNavSegementWidth, LYHomeNavSegementHeight);
    self.navigationItem.titleView = segement;
    //默认选中艺友圈
    [segement selectType:SegementTypeFriend];
    
    //rightItem（主题元素）
    [self upDateTheme];
}


/**
 *  初始化消息改变通知
 */
- (void)setUpMessageNotification{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageChange:) name:KNOTIFICATION_onMesssageChanged object:nil];
    
}

/**
 *  移除通知
 */
- (void)resignMessageNotification{

            [[NSNotificationCenter defaultCenter] removeObserver:self name:KNOTIFICATION_onMesssageChanged object:nil];

}

/**
 *  消息通知触发方法
 */
- (void)messageChange:(NSNotification *)notification{

    LYBadgeBtn *btn  = (LYBadgeBtn *)self.messageItem.customView;
    [btn showBadge];

}

#pragma mark - 点击触发方法
/**
 *  切换主页控制器
 */
- (void)changeControllerWithType:(SegementType)type{
    
    switch (type) {
        case SegementTypeArtist:
            //切换E术家控制器
            self.artistVC.view.frame = self.view.bounds;
            [self.view addSubview:self.artistVC.view];
            [self.friendVC.view removeFromSuperview];
            self.selectedVC = self.artistVC;
            self.navigationItem.rightBarButtonItem = self.messageItem;
            break;
            
        case SegementTypeFriend:
            //切换艺友圈控制器
            self.friendVC.view.frame = self.view.bounds;
            [self.view addSubview:self.friendVC.view];
            [self.artistVC.view removeFromSuperview];
            self.selectedVC = self.friendVC;
            
            
            [self upDateTheme];
            
            break;
            
    }
    
}

/**
 *  城市定位
 */
- (void)homeBarLocationBtnOnclick{
    
    LYCityViewController *cityVC = [[LYCityViewController alloc] init];
    
    cityVC.block = ^(NSString *cityName, NSInteger cityID){
        
        self.city = cityName;
        
    };
    
    [self.navigationController pushViewController:cityVC animated:YES];
}

/**
 *  发帖
 */
- (void)publish{

    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
        LYPublicTextViewController *public = [[LYPublicTextViewController alloc] init];
        [self.navigationController pushViewController:public animated:YES];
    }];
    
   

}

@end
